
import java.io.IOException;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Leon
 */
public class Duck extends Sprite implements Runnable{

    public int CurrentFrame = 0;
    private boolean Running;
    public int SpeedX;
    public int SpeedY;
    public boolean isForward;

        
    public Duck(int X,int Y, int Sqeed) throws IOException
    {
        
        super(Image.createImage("/resource/Duck.png"),25,30);
        defineReferencePixel(12,15);
        setRefPixelPosition(X, Y);
        
        while( SpeedX == 0 && SpeedY == 0)
        {
            SpeedX = GameManager.GetRandom(-Sqeed, Sqeed);
            SpeedY = GameManager.GetRandom(-Sqeed, Sqeed);
            if(SpeedX < 0)
            {
                isForward = false;
            }
            else isForward = true;
        }
        Running = true;
        Thread T = new Thread(this);
        T.start();

    }

    public void NextFrame()
    {
        setFrame(CurrentFrame);
        CurrentFrame ++;
        if(CurrentFrame == 4)
            CurrentFrame = 1;
    }
    
    public void PrevFrame()
    {
        setFrame(CurrentFrame);
        CurrentFrame ++;
        if(CurrentFrame == 8)
            CurrentFrame = 5;
    }
        public void Move()
        {
            move(SpeedX,SpeedY);
            int W = GameManager.Width;
            int H = GameManager.Height;
            int X = getRefPixelX();
            int Y =  getRefPixelY();
            if(X < 16 || X > W - 16)
            {
               SpeedX = -SpeedX;
               if(isForward == true)
               {
                    isForward = false;
                    CurrentFrame = 5;
                }
               else
               {
                   isForward = true;
                   CurrentFrame = 1;
               }
            }
            if(Y < 16 || Y > H - 16)
            {
               SpeedY = -SpeedY;
            }
        /*if(X < -18)
        {
            setRefPixelPosition(W,GameManager.GetRandom(0,H));
            return;
        }
        if(X > W + 18)
        {
            setRefPixelPosition(0,GameManager.GetRandom(0,H));
            return;
        }
        if(Y < -18)
        {
            setRefPixelPosition(GameManager.GetRandom(0,W), H);
            return;
        }
        if(Y > H + 18)
        {
            setRefPixelPosition(GameManager.GetRandom(0,W), 0);
            return;
        }*/
        }
         public void Remove() throws IOException
        {
            GameManager.Duck.remove(this);
            GameManager.DuckDie.append(new DuckDie(getRefPixelX(),getRefPixelY()));
        }

        public void run() {
        while(Running)
        {
            if(isForward == true)
            {
                NextFrame();
            }
            else PrevFrame();
            try {
                Thread.sleep(200);
            } catch (InterruptedException ex) {}
        }
    }
    }
